var web = web || {};
web.AutoDrop = Class.create(); 
web.AutoDrop.prototype = {
	initialize: function(options) {
		var defaults = {
			titleMsg: '',
			maxRow: 10,
			getData: function(){},
			loading: false,
			minChar: 1
		};

		this.settings = $.extend(defaults, options);
		this.element = null;
		this.input = null;
		this.loading = null;
		this.panel = null;
		this.panelMsg = null;
		this.panelList = null;
		this.rows = null;
		this.cursor = -1;
		this.size = 0;
		this.lastVal = '';
	},
	bind: function(elem) {
		var _this = this;
		if (0 == elem.size()) return;
		
		this.element = elem;	
		this.input = this.element.children('input');
		this.loading = this.element.children("em");
		
		this._hideLoading();
		this.input.bind('focus', function(event) {
			_this._init();
		});
	},
	addRowToPanelList: function(text, value) {
		var _this = this;
		if (this.size >= this.settings.maxRow) return false;
		
		var row = $(text);
		row.appendTo(this.panelList);
		row.attr('val', value);
		row.bind('click', {v:value}, function(event) {
			_this._setVal(event.data.v);
			_this._hidePanel();
		});
		this.size++;		
	},
	_init: function(){
		var _this = this;
		this.panel = $('#'+ this._getPanelId());
		if (0 == this.panel.size()) {
			this._createDropListPanel();
			
			this.panelMsg = this.panel.find(".drop_list_msg");
			this.panelList = this.panel.find(".drop_list_body");	
			
			this.input.bind('click', function(event) {
				_this._tryToShowPanel();
				event.stopPropagation();
			});
			
			this.input.bind('keyup', function(event) {
				_this._tryToShowPanel();
				
				if (!_this.rows) return false;
				if ( event.keyCode == 13 ) {//ENTER
					if (_this.cursor > -1 && _this.cursor < _this.size) {
						_this._setVal(_this.rows.eq(_this.cursor).attr('val'));
						_this._hidePanel();
					}
				}
			});
			
			this.input.bind('keydown', function(event) {
				if (_this.size > 0) {
					if( event.keyCode == 40 ) {//key ↓
						var n = (_this.cursor + 1) % _this.size;
						if (-1 != _this.cursor) {
							_this.rows.eq(_this.cursor).removeClass('selected');
						}
						_this.rows.eq(n).addClass('selected');
						_this.cursor = n;
						return false;				
					}
					
					if( event.keyCode == 38 ) {//key ↑
						var p = ((_this.cursor - 1) < 0) ? _this.size - 1 : (_this.cursor - 1);
						if (-1 != _this.cursor) {
							_this.rows.eq(_this.cursor).removeClass('selected');
						}
						_this.rows.eq(p).addClass('selected');
						_this.cursor = p;
						return false;
					}
				}
			});
		} else {
			this._tryToShowPanel();
		}
	},
	_hidePanel: function(){
		this.panel.hide();
	},
	_showPanel: function(){
		var offset = this.element.offset();
		this.panel.css({left:offset.left, top:offset.top + this.element.height() + 1});
		this.panel.show();
	},
	_showLoading: function(){
		if (!this.settings.loading) return false;
		this.loading.css('display', 'inline'); 
	},
	_hideLoading: function(){
		if (!this.settings.loading) return false;
		this.loading.hide();
	},
	_createDropListPanel: function(){
		var _this = this;
		var id = this._getPanelId();
		var html = '<div id="'+ id +'" class="auto_drop_list">' +
					 '	<div class="drop_list_warp">' +
					 '		<div class="drop_list_msg">'+ this.settings.titleMsg +'</div>' +
					 '		<div class="drop_list_body"></div>' +
					 '		<div class="drop_list_page"></div>' +
					 '	</div>' +
					 '</div>';
		this.panel = $(html);
		var body = $('body');
		this.panel.appendTo(body);
		
		this.panel.bind('mouseup', function(event){
			event.stopPropagation();					   
		});
		
		body.bind('mouseup', function(){
			_this._hidePanel();
		});		
	},
	_setVal: function(v) {
		this.input.val(v);
	},
	_clear: function() {
		this.cursor = -1;
		this.size = 0;
		this.panelList.html('');
		this.rows = null;		
	},
	_initPanel: function() {
		var _this = this;
		this.rows = this.panelList.children();
		this.rows.bind('mouseover', function() {
			if (_this.cursor != -1) {
				_this.rows.eq(this.cursor).removeClass('selected');
				_this.cursor = -1;
			}
		});
	},
	_tryToShowPanel: function() {
		var val = this.input.val();
		if (val.length >= this.settings.minChar)	{
			if (val != this.lastVal) {
				this._clear();
				
				this._showLoading();
				this.settings.getData.call(this, val, this.settings.maxRow);
				this._hideLoading();
				
				if (this.size > 0) {
					this._initPanel();
					this._showPanel();
				} else {
					this._hidePanel();
				}
				this.lastVal = val;
			} else {
				if (this.size > 0) {
					this._showPanel();
				}
			}
		} else {
			this._hidePanel();
		}
	},
	_getPanelId: function(){
		return 'div_drop_panel_'+ this.element.attr('id');
	}
};

(function($){  
	$.fn.autoDrop = function(options){ 
		return this.each(function(){
			var drop = new web.AutoDrop(options);
			drop.bind($(this));
		});
	}
})(jQuery); 